package com.demo.ping.utils


import com.demo.ping.orm.constant.MessageConstant
import com.demo.ping.orm.entity.ResultCode
import spock.lang.Specification

class ResultTest extends Specification {

    def "should create a successful Result with data"() {
        given: "A data object and a message"
        String data = "test data"
        String message = "Operation successful"

        when: "Creating a Result with data"
        Result<String> result = Result.data(data, message)

        then: "The result should be successful and contain the correct data"
        result.isSuccess
        result.code == ResultCode.SUCCESS.getCode()
        result.data == data
        result.message == message
        result.timestamp > 0
        result.traceId == "" // Assuming no traceId is set in MDC
    }

    def "should create a Result with default success message"() {
        given: "A data object"
        String data = "test data"

        when: "Creating a Result with data using the default success message"
        Result<String> result = Result.data(data)

        then: "The result should be successful and contain the correct data and default message"
        result.isSuccess
        result.code == ResultCode.SUCCESS.getCode()
        result.data == data
        result.message == MessageConstant.SUCCESS
        result.timestamp > 0
        result.traceId == "" // Assuming no traceId is set in MDC
    }

    def "should create a failed Result"() {
        given: "A failure code and message"
        int code = 10500
        String message = "Operation failed"

        when: "Creating a failed Result"
        Result<String> result = Result.fail(code, message)

        then: "The result should not be successful and contain the correct error message"
        !result.isSuccess
        result.code == code
        result.data == null
        result.message == message
        result.timestamp > 0
        result.traceId == "" // Assuming no traceId is set in MDC
    }

    def "should handle null data in Result"() {
        given: "A null data object"
        String data = null
        String message = "Operation successful"

        when: "Creating a Result with null data"
        Result<String> result = Result.data(data, message)

        then: "The result should indicate that the data is empty"
        result.isSuccess
        result.code == ResultCode.SUCCESS.getCode()
        result.data == null
        result.message == "The bearer data is empty"
        result.timestamp > 0
        result.traceId == "" // Assuming no traceId is set in MDC
    }
}